package com.ck.javacode.Chapter8;

/**
 * @author: caoke
 * @date: 2022/3/12 4:42 下午
 * @description:
 */
public class test {
    public static void main(String[] args) {
        int num[] ={2,3,1,0,2,5,3};
        Solution solution = new Solution();

        System.out.println(solution.duplicate(num));

    }
}

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param numbers int整型一维数组
     * @return int整型
     */
    public void swap1(int[] numbers, int number, int number1) {
        int temp = number;
        number = number1;
        number1 = temp;

    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public int duplicate (int[] numbers) {
        int n  = numbers.length;
        //存在不合法的输入的话输出-1
        for (int num : numbers) {
            if (num < 0 || num >= n) {
                return -1;
            }
        }
        for (int i = 0; i < numbers.length; i++) {
            while(numbers[i] != i && numbers[numbers[i]] != numbers[i]) {
                swap(numbers, i, numbers[i]);
            }
            if (numbers[i] != i )
                return numbers[i];
        }
        return -1;
    }


}